#!/bin/bash
platform="ios";
path="";
moduleName="";
email="";
message=""
countLimit=10
sizeLimit=10   #单位KB
totalSizeLimit=100   #单位KB
thisModuleExceed=0
totalSize=0
imageCount =0 

while getopts "p:m:e:c:s:t:" arg
do
    case $arg in
		p)
			path=$OPTARG;;
        m)
			moduleName=$OPTARG;;
		e)
			email=$OPTARG;;
		c)
			countLimit=$OPTARG;;
		s)
			sizeLimit=$OPTARG;;
		t)
            totalSizeLimit=$OPTARG;;
    esac
done

if [ -z "$moduleName" ]; then
	echo 'moduleName can not be empty'
	exit
fi

moduleName_low=$(echo $moduleName | tr 'A-Z' 'a-z')

echoRed(){
	echo -e "\033[0;31m${1}\033[0m";
}

getFileSize(){
	wc -c < ${1} | sed 's/ //g'
}

getSizeStr(){
	size=$1;
	if [ $size -lt 1024 ]; then
		echo "${size}B"
	elif [ $size -lt $((1024*1024)) ]; then
		echo `echo "scale=2;${size}/1024" | bc`KB
	else
		echo `echo "scale=2;${size}/1024/1024" | bc`MB
	fi
}

read_dir(){
	for file in `ls $1` 
	do
	 if [ -d $1"/"$file ] 
	 then
	 	read_dir $1"/"$file
	 else
		imageCount++;
		size=$(getFileSize $1"/"$file)
		totalSize=$(($totalSize+$size))
		sizeStr=$(getSizeStr $size)
		if [ $size -gt $((sizeLimit*1024)) ]; then
			thisModuleExceed=1;
			temp_message="${file}, size:${sizeStr}<br/>";
			message=$message$temp_message;
			echo -e $temp_message;
		fi
		 echo $1"/"$file 
	 fi
	done
}

echo -e "start scan image, moduleName:${moduleName}, countLimit:${countLimit}, sizeLimit:${sizeLimit}KB, totalSizeLimit:${totalSizeLimit}KB\n"
cd $path

if [ $PLATFORM == 'android' ]; then
	#check image count
	count=`ls ./drawable*/jsbundles_${moduleName_low}* | wc -l | sed 's/ //g'`
	if [ $count -gt $countLimit ]; then
		thisModuleExceed=1;
		limit_message="image count exceed limit, count:${count}, limit:${countLimit}<br/>"
		message=$message$limit_message
		echo -e "image count exceed limit, count:${count}, limit:${countLimit}\n"
	fi

	#check every image size
	echo "images exceed limit:"
	for file in `find ./drawable*/jsbundles_${moduleName_low}*`; do
		size=$(getFileSize $file)
		totalSize=$(($totalSize+$size))
		sizeStr=$(getSizeStr $size)
		if [ $size -gt $((sizeLimit*1024)) ]; then
			thisModuleExceed=1;
			temp_message="${file}, size:${sizeStr}<br/>";
			message=$message$temp_message;
			echo -e $temp_message;
		fi
	done
	if [ $totalSize -gt $((totalSizeLimit*1024)) ]; then
		thisModuleExceed=1;
		totalSizeStr=$(getSizeStr $totalSize)
		temp_message="totalSize:${totalSizeStr}<br/>";
		message=$message$temp_message;
		echo -e $temp_message;
	else
		thisModuleExceed=0;
		totalSizeStr=$(getSizeStr $totalSize)
		echo "totalSize:${totalSizeStr} < ${totalSizeLimit}KB!!!"
		exit 0;
	fi

elif [ $PLATFORM == 'ios' ]; then
	#check every image size
	read_dir("$path/assets")

	#check image count
	if [ $imageCount -gt $countLimit ]; then
		thisModuleExceed=1;
		limit_message="image count exceed limit, count:${count}, limit:${countLimit}<br/>"
		message=$message$limit_message
		echo -e "image count exceed limit, count:${count}, limit:${countLimit}\n"
	fi

	if [ $totalSize -gt $((totalSizeLimit*1024)) ]; then
		thisModuleExceed=1;
		totalSizeStr=$(getSizeStr $totalSize)
		temp_message="totalSize:${totalSizeStr}<br/>";
		message=$message$temp_message;
		echo -e $temp_message;
	else
		thisModuleExceed=0;
		totalSizeStr=$(getSizeStr $totalSize)
		echo "totalSize:${totalSizeStr} < ${totalSizeLimit}KB!!!"
		exit 0;
	fi
fi

 #send mail to users
if [ -n "$message" ]; then
	subject="The number of business images named ${moduleName} exceeds the standard."
    message_warning="<h1 style=\"color:red\">Warning</h1><br/>";
    message=$message_warning$message;
	sh ../scripts/auto-send-mail.sh -s "$subject" -m "$message" -e "$email";
fi
cd - > /dev/null
exit $thisModuleExceed